<?php

// $Id$

require_once('mailer_simple.class.php');
require_once('mail_smtp_settings.class.php');
require_once('mail_attachment.class.php');
require_once('mail_image.class.php');


abstract class Mailer_Advanced extends Mailer_Simple {
	const MSG_TEXT = 0;
	const MSG_HTML = 1;
	const MSG_BOTH = 2;

	private $attachments = array();
	private $images = array();
	private $body_html;
	private $format = Mailer_Advanced::MSG_TEXT;
	private $smtp_settings;
	private $ccs = array();
	private $bccs = array();
	private $return_path;
	private $reply_to;

	private $error_msg;


	function __construct(Mail_SMTP_Settigns $smtp_settings, $message_format = Mailer_Advanced::MSG_TEXT, $log_file) {
		parent::__construct($log_file);

		$this->set_smtp_settings($smtp_settings);
		$this->set_format($message_format);
	}


	public function set_smtp_settings(Mail_SMTP_Settigns $smtp_settings) {
		$this->smtp_settings = $smtp_settings;
	}


	public function get_smtp_settings() {
		return $this->smtp_settings;
	}


	public function set_body_html($body_html, $encoding = 'utf-8') {
		$this->body_html = $body_html;
	}


	public function get_body_html() {
		return $this->body_html;
	}


	public function set_format($format) {
		switch($format) {
			case Mailer_Advanced::MSG_TEXT;
			case Mailer_Advanced::MSG_HTML;
			case Mailer_Advanced::MSG_BOTH;
				$this->format = $format;
				break;
			default:
				throw new Tangra_Module_Exception('Invalid format type: '.$format);
				break;
		}
	}


	public function get_format() {
		return $this->format;
	}



	public function cc_exists($cc) {
		$ret = false;

		foreach ($this->ccs as $key => $cc_local) {
			if ($cc_local == $cc) {
				$ret = $key;
				break;
			}
		}

		return $ret;
	}


	public function add_cc($cc) {
		$ret = false;

		if ($this->ccs_exists($cc) === false) {
			if (is_valid_email_rfc2822($cc)) {
				$this->ccs[] = $cc;
			} else {
				throw new Tangra_Module_Exception('Invalid email: '.$cc);
			}
		}

		return $ret;
	}


	public function remove_cc($cc) {
		$index = $this->ccs_exists($cc);
		if ($index !== false) {
			unset($this->ccs[$index]);
		}

	}


	public function get_ccs() {
		return $this->ccs;
	}


	public function bcc_exists($bcc) {
		$ret = false;

		foreach ($this->bccs as $key => $bcc_local) {
			if ($bcc_local == $bcc) {
				$ret = $key;
				break;
			}
		}

		return $ret;
	}


	public function add_bcc($bcc) {
		$ret = false;

		if ($this->bccs_exists($bcc) === false) {
			if (is_valid_email_rfc2822($bcc)) {
				$this->bccs[] = $bcc;
			} else {
				throw new Tangra_Module_Exception('Invalid email: '.$bcc);
			}
		}

		return $ret;
	}


	public function remove_bcc($bcc) {
		$index = $this->bccs_exists($bcc);
		if ($index !== false) {
			unset($this->bccs[$index]);
		}

	}


	public function get_bccs() {
		return $this->bccs;
	}


	public function attachment_exists(Mail_Attachment $attachment) {
		$ret = false;

		foreach($this->attachments as $key => $local_attachment) {
			if ($local_attachment->get_file() == $attachment->get_file()) {
				$ret = $key;
				break;
			}
		}

		return $ret;
	}


	public function add_attachment(Mail_Attachment $attachment) {
		$ret = false;

		if (!$this->attachment_exists($attachment)) {
			$this->attachments[] = $attachment;
			$ret = true;
		}

		return $ret;
	}


	public function get_attachments() {
		return $this->attachments;
	}


	public function image_exists(Mail_image $image) {
		$ret = false;

		foreach($this->images as $key => $local_image) {
			if ($local_image->get_file() == $image->get_file()) {
				$ret = $key;
				break;
			}
		}

		return $ret;
	}


	public function add_image(Mail_image $image) {
		$ret = false;

		if (!$this->image_exists($image)) {
			$this->images[] = $image;
			$ret = true;
		}

		return $ret;
	}


	public function get_images() {
		return $this->images;
	}


	public function set_error_msg($error_msg) {
		$this->error_msg = $error_msg;
	}


	public function get_error_msg() {
		return $this->error_msg;
	}


	public function set_return_path($return_path) {
		if (is_valid_email_rfc2822($bcc)) {
			$this->return_path = $return_path;
		} else {
			throw new Tangra_Module_Exception('Invalid email in return-path: '.$return_path);
		}
	}


	public function get_return_path() {
		return $this->return_path;
	}


	public function set_reply_to($reply_to) {
		$this->reply_to = $reply_to;
	}


	public function get_reply_to() {
		return $this->reply_to;
	}
}